﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace TestStarter.Model
{
    public class ReportOverviewModel
    {
        # region Private Variables / Methods

        private Nullable<int> _passedItems { get; set; }
        private Nullable<int> _failedItems { get; set; }
        private Nullable<int> _skipedItems { get; set; }
        private Nullable<int> _totalItems { get; set; }
        private Nullable<int> _totalDuration { get; set; }
        private DateTime _testDate { get; set; }
        private ObservableCollection<ChartData> _chartDataList { get; set; }

        private string _getPercentage(Nullable<int> items, Nullable<int> totitems)
        {
            float prcntge = (float)items * 100 / (float)totitems;

            return prcntge.ToString("0.00") + "%";
        }

        # endregion

        public ObservableCollection<TestGroup> TestGroupList { get; set; }

        public ObservableCollection<ChartData> ChartDataList
        {
            get
            {
                if (this._chartDataList == null)
                {
                    this._chartDataList = new ObservableCollection<ChartData>()
                    {
                        new ChartData() { Title = _getPercentage(TotPassedItems, TotalItems) + " Passed", Value = TotPassedItems },
                        new ChartData() { Title = _getPercentage(TotFailedItems, TotalItems) + " Falied", Value = TotFailedItems },
                        new ChartData() { Title = _getPercentage(TotSkipedItems, TotalItems) + " Skiped", Value = TotSkipedItems },
                    };
                }

                return this._chartDataList;
            }
        }

        public Nullable<int> TotPassedItems
        {
            get 
            {
                if (this._passedItems == null)
                {
                    this._passedItems = 0;
                    foreach (var tsg in this.TestGroupList)
                    {
                        this._passedItems += tsg.PassedItems;
                    }
                }

                return this._passedItems;
            }
        }

        public Nullable<int> TotFailedItems
        {
            get
            {
                if (this._failedItems == null)
                {
                    this._failedItems = 0;
                    foreach (var tsg in this.TestGroupList)
                    {
                        this._failedItems += tsg.FailedItems;
                    }
                }

                return this._failedItems;
            }
        }

        public Nullable<int> TotSkipedItems
        {
            get
            {
                if (this._skipedItems == null)
                {
                    this._skipedItems = 0;

                    foreach (var tsg in this.TestGroupList)
                    {
                        this._skipedItems += tsg.SkipedItems;
                    }
                }

                return this._skipedItems;
            }
        }

        public Nullable<int> TotalItems
        {
            get
            {
                if (this._totalItems == null)
                {
                    this._totalItems = TotPassedItems + TotFailedItems + TotSkipedItems;
                }

                return this._totalItems;
            }
        }

        public Nullable<int> TotalDuration
        {
            get
            {
                if (this._totalDuration == null)
                {
                    this._totalDuration = 0;
                    foreach (var tsg in this.TestGroupList)
                    {
                        this._totalDuration += tsg.Duration;
                    }
                }

                return this._totalDuration;
            }
        }

        public string SuccessRate
        {
            get
            {
                return _getPercentage(TotPassedItems, TotalItems);
            }
        }

        public string TestDate 
        {
            get
            {
                if (_testDate == new DateTime())
                {
                    _testDate = DateTime.Today;

                    foreach (var tsrg in TestGroupList)
                    {
                        if (DateTime.Compare(_testDate, tsrg.GroupTestStartDate) > 0)
                            _testDate = tsrg.GroupTestStartDate;

                    }
                }
                return _testDate.ToString("MMM dd, yyyy");
            }
        }

    }
}
